package com.weixue.controller.mall;

import com.weixue.common.Constants;
import com.weixue.common.ServiceResultEnum;
import com.weixue.controller.vo.NongFuMallShoppingCartItemVO;
import com.weixue.controller.vo.NongFuMallUserVO;
import com.weixue.entity.NongFuMallShoppingCartItem;
import com.weixue.service.NongFuMallShoppingCartService;
import com.weixue.utils.Result;
import com.weixue.utils.ResultGenerator;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Day36
 *      购物车控制层
 */
@Controller
public class ShoppingCartController {

    @Resource
    private NongFuMallShoppingCartService nongFuMallShoppingCartService;

    /**
     * 2.获取我的购物车中的列表数据
     */
    @GetMapping("/shop-cart")
    public String cartListPage(HttpServletRequest request,
                               HttpSession httpSession) {
        NongFuMallUserVO user = (NongFuMallUserVO) httpSession.getAttribute(Constants.MALL_USER_SESSION_KEY);
        int itemsTotal = 0;
        int priceTotal = 0;
        List<NongFuMallShoppingCartItemVO> myShoppingCartItems = nongFuMallShoppingCartService.getMyShoppingCartItems(user.getUserId());
        if (!CollectionUtils.isEmpty(myShoppingCartItems)) {
            //购物项总数
            itemsTotal = myShoppingCartItems.stream().mapToInt(NongFuMallShoppingCartItemVO::getGoodsCount).sum();
            if (itemsTotal < 1) {
                return "error/error_5xx";
            }
            //总价
            for (NongFuMallShoppingCartItemVO nongFuMallShoppingCartItemVO : myShoppingCartItems) {
                priceTotal += nongFuMallShoppingCartItemVO.getGoodsCount() * nongFuMallShoppingCartItemVO.getSellingPrice();
            }
            if (priceTotal < 1) {
                return "error/error_5xx";
            }
        }
        request.setAttribute("itemsTotal", itemsTotal);
        request.setAttribute("priceTotal", priceTotal);
        request.setAttribute("myShoppingCartItems", myShoppingCartItems);
        return "mall/cart";
    }

    /**
     * 1.加入购物车
     *      在detail.html商品详情页面有加入购物车传输接收的参数为goodsId字段和goodsCount字段
     */
    @PostMapping("/shop-cart")
    @ResponseBody
    public Result saveNongFuMallShoppingCartItem(@RequestBody NongFuMallShoppingCartItem nongFuMallShoppingCartItem,
                                                 HttpSession httpSession) {
        NongFuMallUserVO user = (NongFuMallUserVO) httpSession.getAttribute(Constants.MALL_USER_SESSION_KEY);
        nongFuMallShoppingCartItem.setUserId(user.getUserId());
        //todo 判断数量
        String saveResult = nongFuMallShoppingCartService.saveNongFuMallCartItem(nongFuMallShoppingCartItem);
        //添加成功
        if (ServiceResultEnum.SUCCESS.getResult().equals(saveResult)) {
            return ResultGenerator.genSuccessResult();
        }
        //添加失败
        return ResultGenerator.genFailResult(saveResult);
    }

    /**
     * 3.修改购物车
     */
    @PutMapping("/shop-cart")
    @ResponseBody
    public Result updateNongFuMallShoppingCartItem(@RequestBody NongFuMallShoppingCartItem nongFuMallShoppingCartItem,
                                                   HttpSession httpSession) {
        NongFuMallUserVO user = (NongFuMallUserVO) httpSession.getAttribute(Constants.MALL_USER_SESSION_KEY);
        nongFuMallShoppingCartItem.setUserId(user.getUserId());
        //todo 判断数量
        String updateResult = nongFuMallShoppingCartService.updateNongFuMallCartItem(nongFuMallShoppingCartItem);
        //修改成功
        if (ServiceResultEnum.SUCCESS.getResult().equals(updateResult)) {
            return ResultGenerator.genSuccessResult();
        }
        //修改失败
        return ResultGenerator.genFailResult(updateResult);
    }

    /**
     * 4.删除购物项
     */
    @DeleteMapping("/shop-cart/{nongFuMallShoppingCartItemId}")
    @ResponseBody
    public Result updateNongFuMallShoppingCartItem(@PathVariable("nongFuMallShoppingCartItemId") Long nongFuMallShoppingCartItemId,
                                                   HttpSession httpSession) {
        NongFuMallUserVO user = (NongFuMallUserVO) httpSession.getAttribute(Constants.MALL_USER_SESSION_KEY);
        Boolean deleteResult = nongFuMallShoppingCartService.deleteById(nongFuMallShoppingCartItemId);
        //删除成功
        if (deleteResult) {
            return ResultGenerator.genSuccessResult();
        }
        //删除失败
        return ResultGenerator.genFailResult(ServiceResultEnum.OPERATE_ERROR.getResult());
    }

    /**
     * Day37
     *      订单确认
     *          查询商品信息并计算总价，然后将priceTotal和myShoppingCartItems两个对象放入request请求中
     */
    @GetMapping("/shop-cart/settle")
    public String settlePage(HttpServletRequest request,
                             HttpSession httpSession) {
        int priceTotal = 0;
        NongFuMallUserVO user = (NongFuMallUserVO) httpSession.getAttribute(Constants.MALL_USER_SESSION_KEY);
        List<NongFuMallShoppingCartItemVO> myShoppingCartItems = nongFuMallShoppingCartService.getMyShoppingCartItems(user.getUserId());
        if (CollectionUtils.isEmpty(myShoppingCartItems)) {
            //无数据则不跳转至结算页
            return "/shop-cart";
        } else {
            //总价
            for (NongFuMallShoppingCartItemVO nongFuMallShoppingCartItemVO : myShoppingCartItems) {
                priceTotal += nongFuMallShoppingCartItemVO.getGoodsCount() * nongFuMallShoppingCartItemVO.getSellingPrice();
            }
            if (priceTotal < 1) {
                return "error/error_5xx";
            }
        }
        request.setAttribute("priceTotal", priceTotal);
        request.setAttribute("myShoppingCartItems", myShoppingCartItems);
        return "mall/order-settle";
    }
}
